package ro.imc.app.wifi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

public class WelcomeActivity extends Activity {
	private Button addNetworkBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		
		final ListView listView = (ListView)findViewById(R.id.listView1);
		String[] values = new String[] {"Scottish Pub", "Oldies Pub", "Music Pub",
										"Bohemian Flow", "Kultur Cafe", "Pardon Cafe",
										"The Shelter", "Flying Circus Pub", "Gaura in Zid"};
		
	
		final List<String> list = new ArrayList<String>();
		for(int i = 0; i < values.length; i++){
			list.add(values[i]);
		}
		
		final StableArrayAdapter  adapter = new StableArrayAdapter (this, android.R.layout.simple_list_item_1, list);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

	
			@Override
			public void onItemClick(AdapterView<?> parent, final View view, int position,
					long id) {
				 final String item = (String) parent.getItemAtPosition(position);
			        view.animate().setDuration(2000).alpha(0)
			            .withEndAction(new Runnable() {
			              @Override
			              public void run() {
			                list.remove(item);
			                adapter.notifyDataSetChanged();
			                view.setAlpha(1);
			}	
			});
		
	}
		});
	
	
	addNetwork();
	
	}
	
	private void addNetwork(){
		addNetworkBtn = (Button)findViewById(R.id.addNetworkButton);
		addNetworkBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(WelcomeActivity.this);
				builder.setTitle("Wifi Passw - Sign In");
				
				LayoutInflater inflater = WelcomeActivity.this.getLayoutInflater();
				builder.setView(inflater.inflate(R.layout.add_network_layout, null))
				
				.setPositiveButton("Save",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, close
						// current activity
						
						Intent intent = new Intent(getApplicationContext(), WelcomeActivity.class);
						startActivity(intent);
					}
				  })
				  
				.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
					}
				});
				
				AlertDialog alertDialog = builder.create();
				 
				// show it
				alertDialog.show();
			
			}
			
		});
}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.welcome, menu);
		return true;
	}

}
